'use strict';

const uuidv1 = require('uuid');
const moment = require('moment');
// const sequelize = require("Sequelize");
const cache = require('memory-cache');
const base64url = require("base64url");
const md5Hex = require('md5-hex');

/**工具类
 * 控制器访问方式:ctx.helper.uuidv1()  或 ctx.helper.JSONParse
 */
module.exports = {
  uuidv1,
  moment,
  // sequelize,
  cache,
  base64url,
  md5Hex,
  
  saveCach(param,val){
    this.cache.put(param,val)
  },
  //获取cach
  getCach(param){
    return this.cache.get(param)
  },
  /**
   *返回 二个时间差 毫秒数
   */
  timeDiff(aTime, bTime) {
    let a = moment(aTime);
    let b = moment(bTime);
    return a.diff(b) // 86400000

  },
  /**
   * 用于把md5 密码还原
   */
  restore(str) {
    console.log("要减的",str);
    str = str.substring(0, 2) + str.substring(3, str.length);
    return str;
  },
  /**
   * 
   * @param {*} base 解密
   */
  decode(input) {
    //解
    let _keyStr =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    let output = "";
    let chr1, chr2, chr3;
    let enc1, enc2, enc3, enc4;
    let i = 0;
    input = input.substring(0, 2) + input.substring(3, input.length);
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    while (i < input.length) {
      enc1 = _keyStr.indexOf(input.charAt(i++));
      enc2 = _keyStr.indexOf(input.charAt(i++));
      enc3 = _keyStr.indexOf(input.charAt(i++));
      enc4 = _keyStr.indexOf(input.charAt(i++));
      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;
      output = output + String.fromCharCode(chr1);
      if (enc3 != 64) {
        output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
        output = output + String.fromCharCode(chr3);
      }
    }     
    let substr = output.match(/({=?)(\S*)(?=})/);//去掉前后的乱码
    // console.log("{" + substr[2] + "}");
    output="{" + substr[2] + "}";
    output = this._utf8_decode(output);

    return output;
  },


  _utf8_decode(utftext) {
    let string = "";
    let i = 0;
    let c = 0;
    // let c1 = 0;
    let c2 = 0;
    while (i < utftext.length) {
      c = utftext.charCodeAt(i);
      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      } else if (c > 191 && c < 224) {
        c2 = utftext.charCodeAt(i + 1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      } else {
        c2 = utftext.charCodeAt(i + 1);
        let c3 = utftext.charCodeAt(i + 2);
        string += String.fromCharCode(
          ((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)
        );
        i += 3;
      }
    }
    return string;
  },
  /**
   * 数据加盐
   */
  salting(str) {
    const salt = Math.floor(Math.random() * 10); //一粒盐
    str = str.substring(0, 2) + salt + str.substring(3, str.length);
    return str;
  },
  //数字字符转数字
  parseInt(str) {
    if (typeof str === 'number') return str;
    if (!str) return str;
    return parseInt(str) || 0;
  },
  // 字符串转对象，转换出错返回{}或者默认值
  JSONParse(str, defaultResult) {
    try {
      return JSON.parse(str);
    } catch (e) {
      return defaultResult || {};
    }
  },
};